package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpInvoiceType;
import com.nuoniu.sibanyun.mapper.erp.ErpInvoiceTypeMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpInvoiceTypeService;
import com.nuoniu.sibanyun.service.po.IVPurchaseInvoiceMainService;
import com.nuoniu.sibanyun.service.so.IVSaleInvoiceMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 发票分类 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-02-05
 */
@Service
public class ErpInvoiceTypeServiceImpl extends ServiceImpl<ErpInvoiceTypeMapper, ErpInvoiceType> implements IErpInvoiceTypeService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    @Lazy
    private IVSaleInvoiceMainService saleInvoiceMainService;
    @Autowired
    private IVPurchaseInvoiceMainService purchaseInvoiceMainService;

    @Override
    public void initData(UserInfo userInfo, Integer accountId) {
        baseMapper.initDefErpInvoiceType(userInfo.getCompanyId(),accountId);
    }

    @Override
    public Page<ErpInvoiceType> pageList(Page page, ErpInvoiceType invoiceType) {
        QueryWrapper<ErpInvoiceType> queryWrapper = new QueryWrapper<>();
        UserInfo userInfo = UserThreadLocal.get();
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        queryWrapper.eq("account_id", accountService.getCurrentAccountId());
        if(StringUtils.isNotBlank(invoiceType.getSearchKey())) {
            queryWrapper.and(wrapper -> wrapper.likeRight("code", invoiceType.getSearchKey()).or().like("name", invoiceType.getSearchKey()));
        }
        if(invoiceType.getType() != null) {
            queryWrapper.eq("type", invoiceType.getType());
        }
        return page(page, queryWrapper);
    }

    @Override
    @Transactional
    public void saveInvoiceType(ErpInvoiceType invoiceType) {
        Integer accountId = accountService.getCurrentAccountId();
        Integer companyId = UserThreadLocal.get().getCompanyId();
        ErpInvoiceType typeByName = baseMapper.getByName(invoiceType.getName(), accountId);
        if(typeByName != null && (invoiceType.getId() == null || !typeByName.getId().equals(invoiceType.getId()))) {
            throw new NuoNiuException("名称已存在");
        }

        ErpInvoiceType typeByCode = baseMapper.getByCode(invoiceType.getCode(), accountId);
        if(typeByCode != null && (invoiceType.getId() == null || !typeByCode.getId().equals(invoiceType.getId()))) {
            throw new NuoNiuException("编码已存在");
        }

        if(invoiceType.getId() == null) {
            invoiceType.setAccountId(accountId);
            invoiceType.setCompanyId(companyId);
        }else {
            saleInvoiceMainService.updateInvoiceType(invoiceType);
            purchaseInvoiceMainService.updateInvoiceType(invoiceType);
            invoiceType.setCode(null);
        }
        this.saveOrUpdate(invoiceType);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        Integer count = saleInvoiceMainService.getByInvoiceType(StringUtils.join(ids, ","));
        if(count > 0) {
            throw new NuoNiuException("已在销售发票单据中使用,不能删除");
        }

        Integer purchaseCount = purchaseInvoiceMainService.getByInvoiceType(StringUtils.join(ids, ","));
        if(purchaseCount > 0) {
            throw new NuoNiuException("已在采购发票单据中使用,不能删除");
        }

        this.removeByIds(ids);
    }
}
